class Admin::FeedsController < Admin::AbstractModelController
  model_class Feed
  
  before_filter :init
  
  def index
    @feeds = Feed.find_all
    respond_to do |format|
      format.html
      format.xml  { render :xml => @feeds.to_xml }
    end
  end
  
  def show
    @feed = Feed.find(params[:id])
    respond_to do |format|
      format.html
      format.xml  { render :xml => @feed.to_xml }
    end
  end
  
  def new
    @feed = Feed.new
  end
  
  def edit
    @feed = Feed.find(params[:id])
  end
  
  def create
    @feed = Feed.new(@params['feed'])
    @feed[:url] = @params['feed']['url'].strip if @params['feed']['url']
    respond_to do |format|
      if @feed.save
        announce_saved
        format.html { redirect_to feeds_path }
        format.xml  { head :created }
      else
        announce_validation_errors
        format.html { render :action => "new" }
        format.xml  { render :xml => @feed.errors.to_xml }
      end
    end
  end
  
  def update
    @feed = Feed.find(params[:id])
    respond_to do |format|
      if @feed.update_attributes(params[:feed])
        flash[:notice] = 'Feed was successfully updated.'
        format.html { redirect_to feeds_path }
        format.xml { head :ok }
      else
        # flash[:notice]
        format.html { render :action => "edit" }
        format.xml  { render :xml => @feed.errors.to_xml }
      end
    end
  end
  
  def destroy
    @feed = Feed.find(@params['id'])
    @feed.destroy
    announce_removed
    respond_to do |format|
      format.html { redirect_to feeds_path }
      format.xml  { head :ok }
    end
  end
  
  private
    def init
      include_stylesheet "feedme.css"
    end
end
